package cn.yun.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;


@WebServlet("/sessionDemo3")
public class SessionDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用session共享数据
        //1.获取session
        HttpSession session = request.getSession();
        System.out.println(session);//org.apache.catalina.session.StandardSessionFacade@46c8fd54
                                    //服务器不关闭  重启浏览器后 org.apache.catalina.session.StandardSessionFacade@4c895df7

        //期望客户端关闭后,session也能相同
        Cookie cookie = new Cookie("JSESSIONID", session.getId());
        //设置cookie存活时间为1小时内
        cookie.setMaxAge(60*60);
        response.addCookie(cookie);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
